package com.tbit.uqbike.webmanager.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.*;
import com.tbit.uqbike.object.value.FinaStat;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.value.Page;
import com.tbit.uqbike.object.value.UserRcDetail;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.*;
import com.tbit.uqbike.webmanager.util.*;
import com.tbit.uqbike.webmanager.util.excel.ExportExcelUtil;
import com.tbit.uqbike.webmanager.util.excel.ExportExcelWrapper;
import com.tbit.utils.DateTimeUtil;
import com.tbit.utils.NullUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.RequestContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: chen
 * @Description: 财务统计
 * @Date: 创建于 14:53 2020/6/30
 */
@RestController
@RequestMapping("/finaStat")
public class FinaStatController extends BaseController {
    @Autowired
    private FinaStatService finaStatService;
    @Autowired
    private RideLogService rideLogService;
    @Autowired
    private RideCardService rideCardService;
    @Autowired
    private MemberFeeService memberFeeService;
    @Autowired
    private RechargeService rechargeService;
    @Autowired
    private VipCardService vipCardService;

    public static void main(String[] args) {
        String orderAmount = "29.91";
        Number num = Float.parseFloat(orderAmount) * 100;
        double oamount = num.doubleValue();
        oamount = oamount / 100.0;
        System.out.println(oamount);
    }

    /**
     * 查询订单
     *
     * @param accountIds 区域id
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param request   请求
     */
    @RequestMapping("/groupByAccountId")
    public JsonResult groupByAccountId(String accountIds, Long startTime, Long endTime, HttpServletRequest request) {
        Assert.notNull(startTime, "ver.startTime.notNull");
        Assert.notNull(accountIds, "ver.accountId.notNull");
        Assert.notNull(endTime, "ver.endTime.notNull");
        LoginInfo info = getLogin(request);
        List<FinaStat> list = finaStatService.groupByAccountId(accountIds, startTime, endTime, info);
        return JsonResult.ok(list);
    }

    @RequestMapping("/exportByAccountId")
    public void exportByAccountId(String accountIds, Long startTime, Long endTime, HttpServletRequest request, HttpServletResponse response) {
        Assert.notNull(accountIds, "ver.accountId.notNull");
        Assert.notNull(startTime, "ver.startTime.notNull");
        Assert.notNull(endTime, "ver.endTime.notNull");
        RequestContext context = getLogin(ServletUtils.getRequest()).getContext();
        LoginInfo info = getLogin(request);
        List<FinaStat> list;
        if (info.roleLowerThanFranchisee()) {
            list = Collections.emptyList();
        } else {
            list = finaStatService.groupByAccountId(accountIds, startTime, endTime, info);
        }
        List<String[]> data = FinaExportUtil.getExportContent(list, context);
        ExportExcelWrapper<String[]> export = new ExportExcelWrapper<>();
        export.exportExcel(context.getMessage("excel_head_financial"), context.getMessage("excel_head_financial"), null, data, response, ExportExcelUtil.EXCEL_FILE_2003);
    }

    /**
     * 订单详细分页显示
     *
     * @param accountId 代理id
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param rowCount  每页大小
     * @param pageNO    页码数
     * @return
     */
    @RequestMapping("/rideLogDetail")
    public JsonResult rideLogDetail(@NotNull(message = "ver.areaId.notNull") Integer accountId,
                                    Integer orderPayType, Integer timeType,
                                    @NotNull(message = "ver.startTime.notNull") Long startTime,
                                    @NotNull(message = "ver.endTime.notNull") Long endTime,
                                    @RequestParam(defaultValue = "10") Integer rowCount,
                                    @RequestParam(defaultValue = "1") Integer pageNO) {
        List<RideLog> list = rideLogService.userPage(accountId, orderPayType, timeType, startTime, endTime, pageNO, rowCount);
        Integer total = rideLogService.userCount(accountId, orderPayType, timeType, startTime, endTime);
        MoneyData moneyData = rideLogService.statMoney(accountId, orderPayType, timeType, startTime, endTime);
        return JsonResult.ok(list, total, moneyData.getMoney(), moneyData.getNum());
    }

    /**
     * 订单详细导出
     *
     * @param accountId
     * @param startTime
     * @param endTime
     * @param response
     */
    @RequestMapping("/rideLogExport")
    public void rideLogExport(@NotNull(message = "ver.areaId.notNull") Integer accountId,
                              @NotNull(message = "ver.startTime.notNull") Long startTime,
                              @NotNull(message = "ver.endTime.notNull") Long endTime,
                              Integer orderPayType, Integer timeType, HttpServletResponse response) {
        RequestContext context = getLogin(ServletUtils.getRequest()).context;
        List<RideLog> list = rideLogService.userExport(accountId, orderPayType, timeType, startTime, endTime);
        List<String[]> data = RechargeExportUtil.getRechargeExportContent(list, context);
        ExportExcelWrapper<String[]> export = new ExportExcelWrapper<>();
        export.exportExcel(context.getMessage("excel_head_ridelog_detail") + DateTimeUtil.getNowTimeYMD(), context.getMessage("excel_head_ridelog_detail") + DateTimeUtil.getNowTimeYMD(), null, data, response, ExportExcelUtil.EXCEL_FILE_2003);
    }

    /**
     * 骑行卡详细分页显示
     *
     * @param accountId 代理id
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param rowCount  每页大小
     * @param pageNO    页码数
     * @return
     */
    @RequestMapping("/getRideCardPage")
    public JsonResult getRideCardPage(@NotNull(message = "ver.areaId.notNull") Integer accountId, @NotNull(message = "ver.startTime.notNull") Long startTime, @NotNull(message = "ver.endTime.notNull") Long endTime, Integer type, @RequestParam(defaultValue = "10") Integer rowCount,
                                      @RequestParam(defaultValue = "1") Integer pageNO) {
        List<UserRcDetail> list = rideCardService.getRideCardPage(accountId, type, startTime, endTime, pageNO, rowCount);
        Integer total = rideCardService.getRideCardCount(accountId, type, startTime, endTime);
        MoneyData moneyData = rideCardService.getTotalMoney(accountId, type, startTime, endTime);
        return JsonResult.ok(list, total, moneyData.getMoney(), moneyData.getNum());
    }

    /**
     * 骑行卡详细导出
     *
     * @param accountId
     * @param startTime
     * @param endTime
     * @param response
     */
    @RequestMapping("/rideCardExport")
    public void rideCardExport(@NotNull(message = "ver.areaId.notNull") Integer accountId, @NotNull(message = "ver.startTime.notNull") Long startTime, @NotNull(message = "ver.endTime.notNull") Long endTime, Integer type, HttpServletResponse response) {
        RequestContext context = getLogin(ServletUtils.getRequest()).getContext();
        Integer total = rideCardService.getRideCardCount(accountId, type, startTime, endTime);
        List<UserRcDetail> list = rideCardService.export(accountId, type, startTime, endTime);
        List<String[]> data = RechargeExportUtil.rideCardContent(list, context);
        ExportExcelWrapper<String[]> export = new ExportExcelWrapper<>();
        export.exportExcel(context.getMessage("excel_head_ridecard_detail") + DateTimeUtil.getNowTimeYMD(), context.getMessage("excel_head_ridecard_detail") + DateTimeUtil.getNowTimeYMD(), null, data, response, ExportExcelUtil.EXCEL_FILE_2003);
    }

    /**
     * @param accountId
     * @param type
     * @param startTime
     * @param endTime
     * @return com.tbit.uqbike.object.view.JsonResult
     * @Description: 骑行卡分类统计
     * @createDate 2020/10/29 15:51
     */
    @RequestMapping("/rideCardCountByType")
    public JsonResult rideCardCountByType(@NotNull(message = "ver.areaId.notNull") Integer accountId, @NotNull(message = "ver.startTime.notNull") Long startTime, @NotNull(message = "ver.endTime.notNull") Long endTime, Integer type) {
        List<Map<String, Object>> lists = rideCardService.rideCardCountByType(new HashMap<>(), accountId, type, startTime, endTime);
        return JsonResult.ok(lists);
    }

    /**
     * 免押卡详细分页显示
     *
     * @param accountId 代理id
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param rowCount  每页大小
     * @param pageNO    页码数
     * @return
     */
    @RequestMapping("/getMemPage")
    public JsonResult getMemPage(@NotNull(message = "ver.areaId.notNull") Integer accountId, @NotNull(message = "ver.startTime.notNull") Long startTime, @NotNull(message = "ver.endTime.notNull") Long endTime, Integer type, @RequestParam(defaultValue = "10") Integer rowCount,
                                 @RequestParam(defaultValue = "1") Integer pageNO) {
        List<UserRcDetail> list = memberFeeService.getMemPage(accountId, type, startTime, endTime, pageNO, rowCount);
        Integer total = memberFeeService.getMemCount(accountId, type, startTime, endTime);
        MoneyData moneyData = memberFeeService.getTotalMoney(accountId, type, startTime, endTime);
        return JsonResult.ok(list, total, moneyData.getMoney(), moneyData.getNum());
    }

    /**
     * 免押卡详细导出
     *
     * @param accountId
     * @param startTime
     * @param endTime
     * @param response
     */
    @RequestMapping("/memExport")
    public void memExport(@NotNull(message = "ver.areaId.notNull") Integer accountId, @NotNull(message = "ver.startTime.notNull") Long startTime, @NotNull(message = "ver.endTime.notNull") Long endTime, Integer type, HttpServletResponse response) {
        RequestContext context = getLogin(ServletUtils.getRequest()).getContext();
        List<UserRcDetail> list = memberFeeService.export(accountId, type, startTime, endTime);
        List<String[]> data = RechargeExportUtil.rideCardContent(list, context);
        ExportExcelWrapper<String[]> export = new ExportExcelWrapper<>();
        export.exportExcel(context.getMessage("excel_head_membercard_detail") + DateTimeUtil.getNowTimeYMD(), context.getMessage("excel_head_membercard_detail") + DateTimeUtil.getNowTimeYMD(), null, data, response, ExportExcelUtil.EXCEL_FILE_2003);
    }

    /**
     * @param accountId
     * @param type
     * @param startTime
     * @param endTime
     * @return com.tbit.uqbike.object.view.JsonResult
     * @Description: 免押卡分类统计购买记录
     * @createDate 2020/10/29 16:07
     */
    @RequestMapping("/memCountByType")
    public JsonResult memCountByType(@NotNull(message = "ver.areaId.notNull") Integer accountId, @NotNull(message = "ver.startTime.notNull") Long startTime, @NotNull(message = "ver.endTime.notNull") Long endTime, Integer type) {
        List<Map<String, Object>> mems = memberFeeService.memCountByType(new HashMap<>(), accountId, type, startTime, endTime);
        return JsonResult.ok(mems);
    }

    /**
     * 押金详细分页显示
     *
     * @param accountId 代理id
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param rowCount  每页大小
     * @param pageNO    页码数
     * @return
     */
    @RequestMapping("/getDepositPage")
    public JsonResult getDepositPage(Integer accountId, Long startTime, Long endTime, @RequestParam(defaultValue = "10") Integer rowCount,
                                     @RequestParam(defaultValue = "1") Integer pageNO, HttpServletRequest request) {
        LoginInfo loginInfo = getLogin(request);
        if (loginInfo.type.equals(UserConstant.USER_BRAND)) {
            accountId = loginInfo.id;
        } else {
            Assert.notNull(accountId, "ver.accountId.notNull");
        }
        Integer total = finaStatService.queryCount(accountId, startTime, endTime);
        if (total <= 0) {
            return JsonResult.ok(Collections.emptyList(), total);
        }
        List<WeixinDepositLog> list = finaStatService.queryPage(accountId, startTime, endTime, pageNO, rowCount);
        return JsonResult.ok(list, total);
    }

    /**
     * @param accountId
     * @param startTime
     * @param endTime
     * @param rowCount
     * @param pageNO
     * @return com.tbit.uqbike.object.view.JsonResult
     * @Description: 区域押金列表
     * @createDate 2020/12/28 16:29
     */
    @PostMapping("/getDepositByArea")
    public JsonResult getDepositByArea(Integer accountId, Long startTime, Long endTime, @RequestParam(defaultValue = "10") Integer rowCount,
                                       @RequestParam(defaultValue = "1") Integer pageNO) {
        Assert.notNull(accountId, "ver.areaId.notNull");
        Assert.notNull(startTime, "sugar.startRideDt.notNull");
        Assert.notNull(endTime, "sugar.endRideDt.notNull");
        PageInfo<WeixinDepositLog> info = new PageInfo<>(finaStatService.getDepositByArea(new HashMap<>(), accountId, startTime, endTime, rowCount, pageNO, true));
        int money = finaStatService.getDepositCountByArea(new HashMap<>(), accountId, startTime, endTime);
        return JsonResult.ok(info.getList(), (int) info.getTotal(), money, (int) info.getTotal());
    }

    /**
     * @param accountId
     * @param startTime
     * @param endTime
     * @param response
     * @return void
     * @Description: 区域押金详细导出
     * @createDate 2020/12/28 16:29
     */
    @GetMapping("/exportDepositByArea")
    public void exportDepositByArea(Integer accountId, Long startTime, Long endTime, HttpServletResponse response) {
        Assert.notNull(accountId, "ver.areaId.notNull");
        Assert.notNull(startTime, "sugar.startRideDt.notNull");
        Assert.notNull(endTime, "sugar.endRideDt.notNull");
        RequestContext context = getLogin(ServletUtils.getRequest()).getContext();
        List<WeixinDepositLog> depositByArea = finaStatService.getDepositByArea(new HashMap<>(), accountId, startTime, endTime, 0, 0, false);
        List<String[]> data = RechargeExportUtil.exportDeposit(depositByArea, context);
        ExportExcelWrapper<String[]> export = new ExportExcelWrapper<>();
        export.exportExcel(context.getMessage("excel_head_area_deposit") + DateTimeUtil.getNowTimeYMD(), context.getMessage("excel_head_area_deposit") + DateTimeUtil.getNowTimeYMD(), null, data, response, ExportExcelUtil.EXCEL_FILE_2003);
    }

    /**
     * 押金详细导出
     *
     * @param accountId
     * @param startTime
     * @param endTime
     * @param response
     */
    @RequestMapping("/depositExport")
    public void depositExport(Integer accountId, Long startTime, Long endTime, HttpServletResponse response, HttpServletRequest request) {
        LoginInfo loginInfo = getLogin(request);
        RequestContext context = loginInfo.getContext();
        if (loginInfo.type.equals(UserConstant.USER_BRAND)) {
            accountId = loginInfo.id;
        } else {
            Assert.notNull(accountId, "ver.accountId.notNull");
        }
        Integer total = finaStatService.queryCount(accountId, startTime, endTime);
        List<WeixinDepositLog> list = finaStatService.export(accountId, startTime, endTime);
        List<String[]> data = RechargeExportUtil.depositContent(list, context);
        ExportExcelWrapper<String[]> export = new ExportExcelWrapper<>();
        export.exportExcel(context.getMessage("excel_head_deposit") + DateTimeUtil.getNowTimeYMD(), context.getMessage("excel_head_deposit") + DateTimeUtil.getNowTimeYMD(), null, data, response, ExportExcelUtil.EXCEL_FILE_2003);
    }

    /**
     * 查询时间段内押金总数及总金额，退还押金总数及总金额
     *
     * @param accountId
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping("/getSum")
    public JsonResult getSum(Integer accountId, Long startTime, Long endTime) {
        Assert.notNull(accountId, "ver.accountId.notNull");
        JSONObject jsonObject = finaStatService.getSum(accountId, startTime, endTime);
        return JsonResult.ok(jsonObject);
    }

    /**
     * 区域用户余额详细分页显示
     *
     * @param accountId 品牌id
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param rowCount  每页大小
     * @param pageNO    页码数
     * @return
     */
    @PostMapping("/getUserPage")
    public JsonResult getBalanceByArea(Integer accountId, Long startTime, Long endTime, @RequestParam(defaultValue = "10") Integer rowCount,
                                       @RequestParam(defaultValue = "1") Integer pageNO) {
        Assert.notNull(accountId, "ver.areaId.notNull");
        Assert.notNull(startTime, "sugar.startRideDt.notNull");
        Assert.notNull(endTime, "sugar.endRideDt.notNull");
        PageInfo<User> pageInfo = new PageInfo<>(rechargeService.getBalanceByArea(new HashMap<>(), accountId, startTime, endTime, rowCount, pageNO, true));
        Map<String, Object> map = rechargeService.getBalanceCountByArea(accountId);
        return JsonResult.ok(pageInfo.getList(), (int) pageInfo.getTotal(), Integer.valueOf(map.get("money") + ""), (int) pageInfo.getTotal());
    }

    /**
     * 区域用户余额详细导出
     *
     * @param accountId
     * @param startTime
     * @param endTime
     * @param response
     */
    @GetMapping("/userExport")
    public void userExport(Integer accountId, Long startTime, Long endTime, HttpServletResponse response) {
        Assert.notNull(accountId, "ver.areaId.notNull");
        Assert.notNull(startTime, "sugar.startRideDt.notNull");
        Assert.notNull(endTime, "sugar.endRideDt.notNull");
        RequestContext context = getLogin(ServletUtils.getRequest()).getContext();
        List<User> users = rechargeService.getBalanceByArea(new HashMap<>(), accountId, startTime, endTime, 0, 0, false);
        List<String[]> data = RechargeExportUtil.userMoneyContent(users, context);
        ExportExcelWrapper<String[]> export = new ExportExcelWrapper<>();
        export.exportExcel(context.getMessage("excel_head_money_detail") + DateTimeUtil.getNowTimeYMD(), context.getMessage("excel_head_money_detail") + DateTimeUtil.getNowTimeYMD(), null, data, response, ExportExcelUtil.EXCEL_FILE_2003);
    }

    /**
     * 用户充值统计分页显示
     *
     * @param accountId 品牌id
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @RequestMapping("/getRechargePage")
    public JsonResult getRechargePage(Integer accountId, String orderNO, Long startTime, Integer rechargeType, Long endTime,
                                      String phone, Integer rowCount, Integer pageNO, Integer adAccountId) {
        Assert.notNull(accountId, "ver.areaId.notNull");
        Assert.notNull(startTime, "sugar.startRideDt.notNull");
        Assert.notNull(endTime, "sugar.endRideDt.notNull");
        List<Recharge> list = rechargeService.queryPage(accountId, orderNO, rechargeType, startTime, endTime, phone, adAccountId, pageNO, rowCount);
        Integer total = rechargeService.queryCount(accountId, orderNO, rechargeType, startTime, endTime, phone, adAccountId);
        return JsonResult.ok(list, total);
    }

    /**
     * @param accountId    品牌id
     * @param startTime
     * @param rechargeType 充值类型
     * @param endTime
     * @param request
     * @return com.tbit.uqbike.object.view.JsonResult
     * @Description: 获取充值总额和订单总数
     * @createDate 2020/10/23 14:19
     */
    @RequestMapping("/getRechargeTotal")
    public JsonResult getRechargeTotal(Integer accountId, Long startTime, Integer rechargeType, Long endTime, HttpServletRequest request) {
        Assert.notNull(accountId, "ver.areaId.notNull");
        Assert.notNull(startTime, "sugar.startRideDt.notNull");
        Assert.notNull(endTime, "sugar.endRideDt.notNull");
        JSONObject jsonObject = rechargeService.getRechargeTotal(accountId, rechargeType, startTime, endTime);
        return JsonResult.ok(jsonObject);
    }

    /**
     * 用户充值统计详细导出
     *
     * @param accountId
     * @param startTime
     * @param endTime
     * @param response
     */
    @RequestMapping("/rechargeExport")
    public void rechargeExport(Integer accountId, String orderNO, Integer rechargeType, String phone, Integer areaId, Long startTime, Long endTime, HttpServletResponse response, HttpServletRequest request, Integer adAccountId) {
        LoginInfo loginInfo = getLogin(request);
        RequestContext context = loginInfo.getContext();
        if (loginInfo.type.equals(UserConstant.USER_BRAND)) {
            accountId = loginInfo.id;
        } else {
            Assert.notNull(accountId, "ver.accountId.notNull");
        }
        List<Recharge> list = rechargeService.export(accountId, orderNO, rechargeType, phone, areaId, startTime, endTime, adAccountId);

        /**导出数据限制最大20000条*/
        if (list.size() > 20000) {
            throw new BaseException("ver.size.too.large");
        }
        List<String[]> data = RechargeExportUtil.rechargeContent(list, context);
        ExportExcelWrapper<String[]> export = new ExportExcelWrapper<>();
        export.exportExcel(context.getMessage("excel_head_money_detail") + DateTimeUtil.getNowTimeYMD(), context.getMessage("excel_head_money_detail") + DateTimeUtil.getNowTimeYMD(), null, data, response, ExportExcelUtil.EXCEL_FILE_2003);
    }

    /**
     * 会员卡分页
     *
     * @param accountId
     * @param type
     * @param startTime
     * @param endTime
     * @param rowCount
     * @param pageNO
     * @param request
     * @return
     */
    @RequestMapping("/getVipPage")
    public JsonResult getVipPage(Integer accountId, Integer type, Long startTime, Long endTime, @RequestParam(defaultValue = "10") Integer rowCount,
                                 @RequestParam(defaultValue = "1") Integer pageNO, HttpServletRequest request) {
        Assert.notNull(accountId, "ver.areaId.notNull");
        Assert.notNull(startTime, "sugar.startRideDt.notNull");
        Assert.notNull(endTime, "sugar.endRideDt.notNull");
        List<UserRcDetail> list = vipCardService.page(accountId, type, startTime, endTime, pageNO, rowCount);
        Integer total = vipCardService.count(accountId, type, startTime, endTime);
        MoneyData moneyData = vipCardService.getTotalMoney(new HashMap<>(), accountId, type, startTime, endTime);
        return JsonResult.ok(list, total, moneyData.getMoney(), moneyData.getNum());
    }

    /**
     * 会员卡收入
     *
     * @param accountId
     * @param type
     * @param startTime
     * @param endTime
     * @param response
     */
    @RequestMapping("/vipExport")
    public void vipExport(Integer accountId, Integer type, Long startTime, Long endTime, HttpServletResponse response) {
        Assert.notNull(accountId, "ver.areaId.notNull");
        Assert.notNull(startTime, "sugar.startRideDt.notNull");
        Assert.notNull(endTime, "sugar.endRideDt.notNull");
        Integer total = vipCardService.count(accountId, type, startTime, endTime);
        RequestContext context = getLogin(ServletUtils.getRequest()).getContext();
        /**导出数据限制最大10000条*/
        if (total > 10000) {
            throw new BaseException("ver.size.too.large");
        }
        List<UserRcDetail> list = vipCardService.export(accountId, type, startTime, endTime);
        List<String[]> exports = RechargeExportUtil.vipContent(list, context);
        ExportExcelWrapper<String[]> export = new ExportExcelWrapper<>();
        export.exportExcel(context.getMessage("excel_head_vipcard_detail") + DateTimeUtil.getNowTimeYMD(), context.getMessage("excel_head_vipcard_detail") + DateTimeUtil.getNowTimeYMD(), null, exports, response, ExportExcelUtil.EXCEL_FILE_2003);
    }

    /**
     * @param accountId
     * @param type
     * @param startTime
     * @param endTime
     * @return com.tbit.uqbike.object.view.JsonResult
     * @Description: 免押卡分类统计购买记录
     * @createDate 2020/10/29 16:07
     */
    @RequestMapping("/vipCountByType")
    public JsonResult vipCountByType(Integer accountId, Integer type, Long startTime, Long endTime) {
        Assert.notNull(accountId, "ver.accountId.notNull");
        List<Map<String, Object>> vips = vipCardService.vipCountByType(new HashMap<>(), accountId, type, startTime, endTime);
        return JsonResult.ok(vips);
    }

    /**
     * @param rideLog
     * @return com.tbit.uqbike.object.view.JsonResult
     * @Description: 欠款分页详情
     * @createDate 2020/12/23 17:17
     */
    @PostMapping("/detailarrears")
    public JsonResult detailarrears(RideLog rideLog) {
        Assert.notNull(rideLog.getAccountId(), "ver.areaId.notNull");
        Assert.notNull(rideLog.getStartTime(), "sugar.startRideDt.notNull");
        Assert.notNull(rideLog.getEndTime(), "sugar.endRideDt.notNull");
        Page page = new Page(rideLog.getPageNO(), rideLog.getRowCount());
        PageHelperUtil.initPage(page);
        rideLog.setPage(page);
        /**用户欠款分页详情*/
        List<Map<String, Object>> pages = rideLogService.arrearsStatistics(rideLog, true);
        PageInfo<Map<String, Object>> info = new PageInfo<>(pages);
        return JsonResult.ok(info.getList(), (int) info.getTotal());
    }

    /**
     * @param rideLog
     * @return com.tbit.uqbike.object.view.JsonResult
     * @Description: 欠款分页详情导出
     * @createDate 2020/12/23 17:17
     */
    @GetMapping("/exportDetailarrears")
    public void exportDetailarrears(RideLog rideLog, HttpServletResponse response) {
        Assert.notNull(rideLog.getAccountId(), "ver.areaId.notNull");
        Assert.notNull(rideLog.getStartTime(), "sugar.startRideDt.notNull");
        Assert.notNull(rideLog.getEndTime(), "sugar.endRideDt.notNull");
        /**用户欠款分页详情*/
        RequestContext context = getLogin(ServletUtils.getRequest()).getContext();
        List<Map<String, Object>> list = rideLogService.arrearsStatistics(rideLog, false);
        if (!NullUtils.isEmpty(list)) {
            for (Map<String, Object> map : list) {
                Double num = Double.parseDouble(map.get("totalAmountOwed").toString());
                Double amount = num.doubleValue();
                amount = amount / 100.0;
                map.put("totalAmountOwed", amount);
            }
        }

        List<String[]> exports = RechargeExportUtil.detailarrearsContent(list, context);
        ExportExcelWrapper<String[]> export = new ExportExcelWrapper<>();
        export.exportExcel(context.getMessage("excel_head_arrears") + DateTimeUtil.getNowTimeYMD(), context.getMessage("excel_head_arrears") + DateTimeUtil.getNowTimeYMD(), null, exports, response, ExportExcelUtil.EXCEL_FILE_2003);
    }

    /**
     * @param rideLog
     * @return com.tbit.uqbike.object.view.JsonResult
     * @Description: 欠款总计
     * @createDate 2020/12/23 17:17
     */
    @PostMapping("/arrearsStatistics")
    public JsonResult arrearsStatistics(RideLog rideLog) {
        Assert.notNull(rideLog.getAccountId(), "ver.areaId.notNull");
        Assert.notNull(rideLog.getStartTime(), "sugar.startRideDt.notNull");
        Assert.notNull(rideLog.getEndTime(), "sugar.endRideDt.notNull");
        rideLog.setPage(new Page(rideLog.getPageNO(), rideLog.getRowCount()));
        PageHelperUtil.initPage(rideLog.getPage());
        List<Map<String, Object>> maps = rideLogService.arrearsStatistics(rideLog, false);
        Long total = 0L;
        for (Map<String, Object> map : maps) {
            Long num = Long.parseLong(map.get("totalAmountOwed").toString());
            total += num;
        }

        Long totalArrears = maps.stream().mapToLong(map -> ((Long) map.get("totalArrears"))).sum();
        Map<Object, Object> map = new HashMap<>();
        map.put("totalAmountOwed", total);
        map.put("totalArrears", totalArrears);
        return JsonResult.ok(map);
    }

}
